package com.example.auv_660.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

public enum FileUtils {
    INSTANCE;

    private FileWrite fileWrite;

    FileUtils() {
        this.fileWrite = new FileWrite();
    }

    public FileWrite getFileWrite() {
        return this.fileWrite;
    }

    public class FileWrite {
        private BufferedWriter bufferedWriterCMD;
        private BufferedWriter bufferedWriterSTATE;
        private BufferedWriter bufferedWriterAUTO;

        private BufferedWriter bufferedWriterDSData;
        private BufferedWriter bufferedWriterDHData;

        private File checkCMDFile;
        private File checkSTATEFile;
        private File checkAUTOFile;

        private File checkDSDATAFile;
        private File checkDHDATAFile;

        private File checkCMDPath;
        private File checkSTATEPath;
        private File checkAUTOPath;

        private File checkDSDATAPath;
        private File checkDHDATAPath;

        FileWrite () {
            createPath(Constants.FILE_PATH);
            createCMDFile(Constants.FILE_PATH);
            createSTATEFile(Constants.FILE_PATH);
            createAUTOFile(Constants.FILE_PATH);
            //createDSDATAFile(Constants.FILE_PATH);
            //createDHDATAFile(Constants.FILE_PATH);
        }

        public void writeCMD(String message) {
            if (!this.checkCMDPath.exists()) {
                createCMDPath(Constants.FILE_PATH);
            }

            if (!this.checkCMDFile.exists()) {
                createCMDFile(Constants.FILE_PATH);
            }

            try {
                this.bufferedWriterCMD.write(message);
                this.bufferedWriterCMD.flush();
            } catch (IOException e) {
                realseWriteCMD();
            }
        }

        private void realseWriteCMD() {
            try {
                if (this.bufferedWriterCMD != null) {
                    this.bufferedWriterCMD.close();
                    this.bufferedWriterCMD = null;
                }
            } catch (IOException e){

            }
        }

        public void writeSTATE(String message) {
            if (!this.checkSTATEPath.exists()) {
                createSTATEPath(Constants.FILE_PATH);
            }
            if (!this.checkSTATEFile.exists()) {
                createSTATEFile(Constants.FILE_PATH);
            }
            try {
                this.bufferedWriterSTATE.write(message);
                this.bufferedWriterSTATE.flush();
            } catch (IOException e) {
                realseWriteSTATE();
            }
        }

        private void realseWriteSTATE() {
            try {
                if (this.bufferedWriterSTATE != null) {
                    this.bufferedWriterSTATE.close();
                    this.bufferedWriterSTATE = null;
                }
            } catch (IOException e) {

            }
        }

        public void writeAUTO(String message) {
            if (!this.checkAUTOPath.exists()) {
                createAUTOPath(Constants.FILE_PATH);
            }

            if (!this.checkAUTOFile.exists()) {
                createAUTOFile(Constants.FILE_PATH);
            }
            try {
                this.bufferedWriterAUTO.write(message);
                this.bufferedWriterAUTO.flush();
            } catch (IOException e) {
                realseWriteAUTO();
            }
        }

        private void realseWriteAUTO() {
            try {
                if (this.bufferedWriterAUTO != null) {
                    this.bufferedWriterAUTO.close();
                    this.bufferedWriterAUTO = null;
                }
            } catch (IOException e) {

            }
        }

        public void writeDSDATA(String message) {
            if (!this.checkDSDATAPath.exists()) {
                createDSDATAPath(Constants.FILE_PATH);
            }

            if (this.checkDSDATAFile == null
                    || !this.checkDSDATAFile.exists()) {
                createDSDATAFile(Constants.FILE_PATH);
            }

            if (this.bufferedWriterDSData == null) {
                createDSDATAFile(Constants.FILE_PATH);
            }

            try {
                this.bufferedWriterDSData.write(message);
                this.bufferedWriterDSData.flush();
            } catch (IOException e) {
                realseWriteDSData();
            }
        }

        public void realseWriteDSData() {
            try {
                if (this.bufferedWriterDSData != null) {
                    this.bufferedWriterDSData.close();
                    this.bufferedWriterDSData = null;
                }
            } catch (IOException e) {

            }
        }

        public void writeDHDATA(String message) {
            if (!this.checkDHDATAPath.exists()) {
                createDHDATAPath(Constants.FILE_PATH);
            }

            if (this.checkDHDATAFile == null
                    || !this.checkDHDATAFile.exists()) {
                createDHDATAFile(Constants.FILE_PATH);
            }

            if (this.bufferedWriterDHData == null) {
                createDHDATAFile(Constants.FILE_PATH);
            }

            try {
                this.bufferedWriterDHData.write(message);
                this.bufferedWriterDHData.flush();
            } catch (IOException e) {
                realseWriteDHData();
            }
        }

        public void realseWriteDHData() {
            try {
                if (this.bufferedWriterDHData != null) {
                    this.bufferedWriterDHData.close();
                    this.bufferedWriterDHData = null;
                }
            } catch (IOException e) {

            }
        }

        private void createPath(String path) {
            File fileCMD = new File(path + Constants.FILE_WRITE_CMD_PATH);
            File fileSTATE = new File(path + Constants.FILE_WRITE_STATE_PATH );
            File fileAUTO = new File(path + Constants.FILE_WRITE_AUTO_PATH);
            File fileDSDATA = new File(path + "AUV_DATA_DS/");
            File fileDHDATA = new File(path + "AUV_DATA_DH/");

            this.checkCMDPath = fileCMD;
            this.checkSTATEPath = fileSTATE;
            this.checkAUTOPath = fileAUTO;
            this.checkDSDATAPath = fileDSDATA;
            this.checkDHDATAPath = fileDHDATA;

            if (!fileCMD.exists()) {
                fileCMD.mkdirs();
            }

            if (!fileSTATE.exists()) {
                fileSTATE.mkdirs();
            }

            if (!fileAUTO.exists()) {
                fileAUTO.mkdir();
            }

            if (!fileDSDATA.exists()) {
                fileDSDATA.mkdir();
            }

            if (!fileDHDATA.exists()) {
                fileDHDATA.mkdir();
            }
        }

        private void createSTATEPath(String path) {
            this.checkSTATEPath = new File(path + Constants.FILE_WRITE_STATE_PATH);
            this.checkSTATEPath.mkdirs();
        }

        private void createCMDPath(String path) {
            this.checkCMDPath = new File(path + Constants.FILE_WRITE_CMD_PATH);
            this.checkCMDPath.mkdirs();
        }

        private void createAUTOPath(String path) {
            this.checkAUTOPath = new File(path + Constants.FILE_WRITE_AUTO_PATH);
            this.checkAUTOPath.mkdirs();
        }

        private void createDSDATAPath(String path) {
            this.checkDSDATAPath = new File(path + "AUV_DATA_DS/");
            this.checkDSDATAPath.mkdir();
        }

        private void createDHDATAPath(String path) {
            this.checkDHDATAPath = new File(path + "AUV_DATA_DH/");
            this.checkDHDATAPath.mkdir();
        }

        private void createCMDFile(String path) {
            String date = Tools.DateFormat(new Date(), "s");

            String fileCMDName = path + Constants.FILE_WRITE_CMD_PATH + date + ".txt";

            this.checkCMDFile = new File(fileCMDName);

            try {
                this.bufferedWriterCMD = new BufferedWriter(new FileWriter(fileCMDName));
            } catch (IOException e) {
                realseWriteCMD();
            }
        }

        private void createSTATEFile(String path) {
            String date = Tools.DateFormat(new Date(), "s");

            String fileSTATEName = path + Constants.FILE_WRITE_STATE_PATH + date + ".txt";

            this.checkSTATEFile = new File(fileSTATEName);

            try {
                this.bufferedWriterSTATE = new BufferedWriter(new FileWriter(fileSTATEName));
            } catch (IOException e) {
                realseWriteSTATE();
            }
        }

        private void createAUTOFile(String path) {
            String date = Tools.DateFormat(new Date(), "s");

            String fileAUTOName = path + Constants.FILE_WRITE_AUTO_PATH + date + ".txt";

            this.checkAUTOFile = new File(fileAUTOName);

            try {
                this.bufferedWriterAUTO = new BufferedWriter(new FileWriter(fileAUTOName));
            } catch (IOException e) {
                realseWriteAUTO();
            }
        }

        private void createDSDATAFile(String path) {
            String date = Tools.DateFormat(new Date(), "s");

            String fileDSDATAName = path + "AUV_DATA_DS/" + Constants.PHONEIMEI + "DS_" + date + ".txt";

            this.checkDSDATAFile = new File(fileDSDATAName);

            try {
                this.bufferedWriterDSData = new BufferedWriter(new FileWriter(fileDSDATAName));
            } catch (IOException e) {

            }
        }

        private void createDHDATAFile(String path) {
            String date = Tools.DateFormat(new Date(), "s");

            String fileDHDATAName = path + "AUV_DATA_DH/" + Constants.PHONEIMEI + "DH_" + date + ".txt";

            this.checkDHDATAFile = new File(fileDHDATAName);

            try {
                this.bufferedWriterDHData = new BufferedWriter(new FileWriter(fileDHDATAName));
            } catch (IOException e) {

            }
        }
    }
}
